﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECommerce.BLL.Product;
using Jzy.Core.Common;
using Jzy.Core.Common.Web;
using Jzy.Core.Common.Basic;
using Jzy.Common;
using ECommerce.Model.Product;
using Jzy.Core.Utility;

namespace ECommerce.Web.ECAdmin.Product
{
    public partial class List : PageList
    {
        private ECProductBO Product
        {
            get
            {
                return ECProductBO.Instance<ECProductBO>();
            }
        }

        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["Name"]))
                {
                    txtName.Text = Request.QueryString["Name"];
                }

                BindView();
            }
            base.InitSelectID(hidSelectIDs);
            AddAttr();
        }

        private void AddAttr()
        {
            lnkAdd.HRef = "Edit.aspx";
            JSUtil.ClickBtn(lnkDelete, btnDelete);
        }

        private void BindView()
        {            
            LVUtil.Init(lv, lvHeader);
            PagerSetting setting = new PagerSetting();
            setting.PageIndex = this.PageIndex;
            setting.PageSize = AppConfig.PageSize;
            setting.OrderBy = "InputTime desc";
            if (txtName.Text.Trim() != "")
            {
                setting.Where = "Name like @p0";
            }

            string prefix = GlobalVar.WebCurrPath + "List.aspx?Name=" + Server.UrlEncode(txtName.Text);

            

            IEnumerable<ECProductInfo> list = this.Product.GetList(setting,"%" + txtName.Text + "%");
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataSource = list;
            lv.DataBind();

            litPager.Text = HtmlPager.Render(setting, prefix, true);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs != "")
            {
                Product.Delete(StrUtil.SplitToInt(strSelectIDs));                
            }
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());
                HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");
                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
                lnkName.NavigateUrl = lnkEdit.NavigateUrl = "Edit.aspx?ID=" + strID;

                HyperLink lnkComment = (HyperLink)e.Item.FindControl("lnkComment");
                lnkComment.NavigateUrl = "CommentList.aspx?ProductID=" + lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();

            }
        }
    }
}